package com.sright.bcard.cms.controller.enterprise;

import com.sright.bcard.common.service.share.dto.enterprise.AllEnterpriseDTO;
import com.sright.bcard.common.service.share.dto.page.BetweenDateParamRequest;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.common.service.share.dto.enterprise.EnterpriseDTO;
import com.sright.bcard.common.service.share.dto.request.CreateAccountParam;
import com.sright.bcard.enterprise.share.request.EnterpriseInfoParam;
import com.sright.bcard.enterprise.share.service.EnterpriseService;
import com.sright.bcard.enterprise.share.service.IndustryService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: lumf
 * Date: 2020/8/12 4:27 PM
 * doc refers to http://eolinker.test.console.ergengtv.com/#/home/project/inside/api/list?groupID=104&childGroupID=107&projectName=%E8%A7%86%E7%84%B6%E5%90%8D%E7%89%87%E6%80%BB%E5%90%8E%E5%8F%B0%20API&projectID=30
 */
@RestController
@RequestMapping("/enterprise/account")
public class EnterpriseManageController {
    @Autowired
    EnterpriseService enterpriseService;

    @Autowired
    IndustryService industryService;

    @GetMapping("/list/get")
    public SrightResponse getEnterprisePageListInDetail(
                @RequestParam(required = false) String keyword,
                @RequestParam(required = false) Integer status,
                @RequestParam(value = "begin_time", required = false) Long beginTime,
                @RequestParam(value = "end_time", required = false) Long endTime,
                Integer page, Integer pageSize) throws ParseException {

        BetweenDateParamRequest betweenDateParamRequest = BetweenDateParamRequest.request(beginTime, endTime);
        PageParamRequest pageParamRequest = PageParamRequest.pageRequest(page, pageSize);
        Pager<AllEnterpriseDTO> pager = enterpriseService.searchEnterpriseByKeyword(keyword, status, betweenDateParamRequest, pageParamRequest);

//        if (pager.getList() != null) {
//            List<Map> result = new ArrayList<>();
//            for (EnterpriseDTO dto : pager.getList()) {
//                Map map = new HashMap();
//                map.put("id", dto.getUserId());
//                map.put("username", dto.getUsername());
//                map.put("status", dto.getStatus());
//                map.put("created_at", dto.getCreatedAt());
//                map.put("expiration_at", dto.getExpirationAt());
//            }
//        }
        return SrightResponse.defaultSuccess(pager);
    }


    @GetMapping("/enterprise/list")
    public SrightResponse getEnterpriseNameListInBrief() {
        List<EnterpriseDTO> list = enterpriseService.getEnterpriseList();
        List<Map<String, Object>> result = new ArrayList<>();
        for (EnterpriseDTO dto : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", dto.getId());
            map.put("name", dto.getName());
            result.add(map);
        }
        return SrightResponse.defaultSuccess(result);
    }

    /**
     *
     * @param id 企业id
     * @return
     */
    @GetMapping("/detail/get")
    public SrightResponse getEnterpriseInDetail(Long id) {
        EnterpriseDTO dto = enterpriseService.getEnterpriseDTO(id);
        return SrightResponse.defaultSuccess(dto);
    }

    @EnableLoginAuth
    @PostMapping("/info/update")
    public SrightResponse updateEnterpriseInfo(@RequestBody EnterpriseInfoParam enterpriseInfoParam) throws ParseException {
        EnterpriseDTO enterpriseDTO = new EnterpriseDTO();
        DataConvertUtils.convertVO2DTO(enterpriseInfoParam, enterpriseDTO);
        Boolean success = enterpriseService.updateEnterprise(enterpriseDTO);
        return SrightResponse.defaultSuccess(success);
    }

    @GetMapping("/industry/list")
    public SrightResponse getIndustryList() {
        return industryService.industryList();
    }


    @EnableLoginAuth
    @PostMapping("/create")
    public SrightResponse addEnterpriseAccount(@RequestBody CreateAccountParam param) {
        enterpriseService.createAccount(param);
        return SrightResponse.defaultSuccess();
    }




}
